package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.MapConstant;
import com.tbit.main.constant.SysConstant;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Machine;
import com.tbit.main.pojo.MachineLastUseLog;
import com.tbit.main.pojo.User;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 设备最后扫码记录
 *
 * @author LMY
 * @create 2021-01-11 10:54
 */
@RestController
@RequestMapping("/machineLastUseLog")
public class MachineLastUseLogController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private MachineLastUseLogService machineLastUseLogService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private UserService userService;

    /**
     * 插入
     *
     * @param token
     * @param userCode
     * @param mapType
     * @param lo
     * @param la
     * @return
     */
    @RequestMapping("/insert")
    public JsonResult insert(String token, String userCode, Integer mapType, Double lo, Double la) {
        Assert.hasLength(userCode, "Machine.userCode.notNull");
        Assert.notNull(lo, "Map.lon.nutNull");
        Assert.notNull(la, "Map.lat.nutNull");

        AccountUser accountUserToken = tokenService.getAccount(token);

        if (mapType == null) {
            mapType = MapConstant.MAP_GOOGLE;
        }

        /**查询设备信息*/
        Machine machine = machineService.getByUseCode(userCode);
        if (machine == null) {
            throw new BaseException("Machine.notExist");
        }

        /**设备权限判断*/
        machineService.auth(accountUserToken.getAgents(), machine.getMachineId());
        MachineLastUseLog machineLastUseLog = new MachineLastUseLog();
        machineLastUseLog.setMachineId(machine.getMachineId());
        machineLastUseLog.setMachineNO(machine.getMachineNO());
        machineLastUseLog.setUserCode(machine.getUserCode());
        machineLastUseLog.setAddTime(DateTimeUtil.getNowTime());
        machineLastUseLog.setUserId(accountUserToken.getAccountUserId());
        machineLastUseLog.setPhone(accountUserToken.getPhone());
        machineLastUseLog.setLa(la);
        machineLastUseLog.setLo(lo);
        machineLastUseLog.setUseType(1);
        calibrateService.calMachineLastUseLog(machineLastUseLog, mapType, SysConstant.ORIGINAL);
        machineLastUseLogService.insert(machineLastUseLog);
        return JsonResult.succ();
    }

    /**
     * 查询
     *
     * @param token
     * @param userCode
     * @param mapType
     * @return
     */
    @RequestMapping("/query")
    public JsonResult query(String token, String userCode, Integer mapType) {
        if (mapType == null) {
            mapType = MapConstant.MAP_GOOGLE;
        }
        Assert.hasLength(userCode, "Machine.userCode.notNull");

        /**查询设备信息*/
        Machine machine = machineService.getByUseCode(userCode);
        if (machine == null) {
            throw new BaseException("Machine.notExist");
        }

        List<MachineLastUseLog> machineLastUseLogs = machineLastUseLogService.query(machine.getMachineId());
        if (CollectionUtils.isEmpty(machineLastUseLogs)){
            return JsonResult.succ(machineLastUseLogs);
        }
        calibrateService.calibrateMachineLastUseLog(machineLastUseLogs, mapType);
        List<Integer> userIdList = machineLastUseLogs.stream().map(MachineLastUseLog::getUserId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        List<User> userList = userService.getUserIds(userIdList);
        if (CollectionUtils.isEmpty(userList)){
            return JsonResult.succ(machineLastUseLogs);
        }
        Map<Integer, User> userMap = userList.stream().collect(Collectors.toMap(User::getUserId, Function.identity()));
        for (MachineLastUseLog machineLastUseLog : machineLastUseLogs) {
            //获取扫码用户名
            User user = userMap.get(machineLastUseLog.getUserId());
            machineLastUseLog.setUserName(Objects.nonNull(user) ? user.getName() : null);
        }
        return JsonResult.succ(machineLastUseLogs);
    }
}
